<template>
  <view class="com-b">
    <view class="com-header">
      <text class="com-name">子组件B</text>
      <text class="com-desc">接收子组件A值 → 回传给父组件</text>
    </view>

    <!-- 兄弟组件值展示区 -->
    <view class="brother-data">
      <text class="data-label">子组件A传递的值：</text>
      <text class="data-value">{{ brotherData || '（等待接收值...）' }}</text>
    </view>

    <!-- 向父组件传值区域 -->
    <view class="to-parent-area">
      <text class="area-title">向父组件回传值</text>
      <input 
        type="text" 
        v-model="toParentValue" 
        class="to-parent-input"
        placeholder="请输入要回传给父组件的值..."
      />
      <button 
        class="send-btn"
        @click="sendToParent"
        :disabled="!toParentValue"
      >
        回传给父组件
      </button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      brotherData: '',
      toParentValue: ''
    }
  },
  onLoad() {
    this.eventHandler = (data) => {
      this.brotherData = data
    }
    uni.$on('a-to-b-event', this.eventHandler)
  },
  onUnload() {
    uni.$off('a-to-b-event', this.eventHandler)
  },
  methods: {
    sendToParent() {
      this.$emit('child-to-parent', this.toParentValue)
      uni.showToast({ title: '已回传给父组件', icon: 'none', duration: 1500 })
      this.toParentValue = ''
    }
  }
}
</script>

<style scoped>
.com-b {
  background-color: #ffffff;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
  border-left: 8rpx solid #3498db; /* 子组件B主题色 */
  padding: 30rpx;
}

.com-header {
  margin-bottom: 28rpx;
}

.com-name {
  font-size: 32rpx;
  font-weight: 600;
  color: #3498db;
  display: block;
  margin-bottom: 8rpx;
}

.com-desc {
  font-size: 24rpx;
  color: #7f8c8d;
}

/* 兄弟组件数据展示 */
.brother-data {
  padding: 20rpx;
  background-color: #f0f7ff;
  border-radius: 8rpx;
  margin-bottom: 28rpx;
}

.data-label {
  font-size: 26rpx;
  color: #666;
}

.data-value {
  font-size: 28rpx;
  color: #2980b9;
  font-weight: 500;
  word-break: break-all;
}

/* 向父组件传值区域 */
.to-parent-area {
  margin-top: 10rpx;
}

.area-title {
  font-size: 26rpx;
  color: #34495e;
  display: block;
  margin-bottom: 16rpx;
}

.to-parent-input {
  width: 100%;
  height: 70rpx;
  border: 1rpx solid #e1e5e8;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
  margin-bottom: 20rpx;
  box-sizing: border-box;
  transition: border-color 0.3s ease;
}

.to-parent-input:focus {
  border-color: #3498db;
  outline: none;
}

.send-btn {
  width: 100%;
  height: 72rpx;
  line-height: 72rpx;
  background-color: #3498db;
  color: #ffffff;
  font-size: 28rpx;
  border-radius: 8rpx;
  border: none;
  transition: all 0.2s ease;
}

.send-btn:active {
  background-color: #2980b9;
  transform: translateY(2rpx);
}

.send-btn:disabled {
  background-color: #bbdefb;
  color: #ffffff;
  cursor: not-allowed;
}
</style>